//
//  UIImageView+Extension.swift
//  新浪微博
//
//  Created by 刘广东 on 16/2/25.
//  Copyright © 2016年 LGD. All rights reserved.
//

import UIKit

extension UIImageView {
    
    /// 使用图像名称创建 UIImageView
    ///
    /// - parameter imageName: imageName
    ///
    /// - returns: UIImageView
    
    convenience init(jm_imageName imageName:String) {
        
        self.init(image: UIImage(named:imageName))
        
    }
    
    /// 设置图像
    ///
    /// - parameter urlString:       urlString
    /// - parameter placeholderName: placeholderName
    func Jm_setImageWithURL(urlString: String?, placeholderName: String?) {
        
        // 占位图像
        var placeholderImage: UIImage?
        
        if placeholderName != nil {
            placeholderImage = UIImage(named: placeholderName!)
        }
        
        // 如果 url 为 nil
        guard let urlString = urlString,
            let url = NSURL(string: urlString) else {
                // 如果 urlString 为nil，但是指定了占位图像，显示占位图像
                image = placeholderImage
                
                return
        }
        
        sd_setImageWithURL(url, placeholderImage: placeholderImage)
    }
    
}